~ chicken-core (master) /manual/Module (chicken port)
Trap1[[tags: manual]]
2[[toc:]]
3
4== Module (chicken port)
5
6This module contains various extended port definitions.
7
8All errors related to failing port operations will signal a condition
9of kind {{exn}}.
10
11* New in CHICKEN 5.4.0: Errors caused by underlying C calls that
12 change errno will produce a condition object with an {{errno}}
13 property, which can be accessed with
14 {{(get-condition-property <the-condition-object> 'exn 'errno)}}.
15
16=== Port attributes
17
18==== port-encoding
19
20<procedure>(port-encoding PORT)</procedure>
21
22Returns the encoding used for reading and writing data from and to
23the given port. Encoding is currently one of the symbols {{utf-8}},
24{{latin-1}} or {{binary}}. Note that the encoding can be changed by using
25the (SRFI-17) setter procedure for this operation:
26
27{{(set! (port-encoding PORT) ENCODING)}}
28
29==== port-name
30
31<procedure>(port-name [PORT])</procedure>
32
33Fetch filename from {{PORT}}. This returns the filename that was used to open
34this file. Returns a special tag string, enclosed into parentheses for
35non-file ports. {{PORT}} defaults to the value of {{(current-input-port)}}.
36
37Note that the encoding can be changed by using
38the (SRFI-17) setter procedure for this operation:
39
40{{(set! (port-name PORT) STRING)}}
41
42
43==== port-position
44
45<procedure>(port-position [PORT])</procedure>
46
47Returns the current position of {{PORT}} as two values: row and column number.
48If the port does not support such an operation an error is signaled. This
49procedure is currently only available for input ports. {{PORT}} defaults to the
50value of {{(current-input-port)}}.
51
52
53=== Setting the file buffering mode
54
55==== set-buffering-mode!
56
57<procedure>(set-buffering-mode! PORT MODE [BUFSIZE])</procedure>
58
59Sets the buffering-mode for the file associated with {{PORT}} to
60{{MODE}}, which should be one of the keywords {{#:full}},
61{{#:line}} or {{#:none}}. If {{BUFSIZE}} is specified it
62determines the size of the buffer to be used (if any).
63
64This procedure doesn't work on custom ports, such as those created with {{make-input-port}} or {{make-output-port}}.
65
66=== Terminal ports
67
68==== terminal-name
69
70<procedure>(terminal-name PORT)</procedure>
71
72Returns the name of the terminal that is connected to {{PORT}}.
73
74On Windows, this procedure always raises an exception.
75
76==== terminal-port?
77
78<procedure>(terminal-port? PORT)</procedure>
79
80Returns {{#t}} if {{PORT}} is connected to a terminal and
81{{#f}} otherwise.
82
83
84==== terminal-size
85
86<procedure>(terminal-size PORT)</procedure>
87
88Returns two values, the number of rows and columns of the terminal
89that is connected to {{PORT}} or {{0}}, {{0}} if the terminal size can
90not be obtained.
91
92On Windows, this procedure always raises an exception.
93
94
95=== Input/output port extensions
96
97==== with-output-to-port
98
99<procedure>(with-output-to-port PORT THUNK)</procedure>
100
101Call procedure {{THUNK}} with the current output-port temporarily
102bound to {{PORT}}.
103
104==== make-input-port
105
106<procedure>(make-input-port READ-CHAR CHAR-READY? CLOSE #!key peek-char read-bytevector read-line)</procedure>
107
108Returns a custom input port. Common operations on this port are
109handled by the given parameters, which should be procedures of no
110arguments. The following arguments are all different kinds of reader
111procedures:
112
113* {{READ-CHAR}} is the most fundamental reader, and must always be
114present. It is a thunk which is called when the next character is
115to be read and it should return a character or {{#!eof}}.
116* {{CHAR-READY?}} is a thunk which is called when {{char-ready?}}
117is called on this port and should return {{#t}} or {{#f}}.
118* {{CLOSE}} is a thunk which is called when the port is closed.
119* {{peek-char}} is a thunk which is called when {{peek-char}} is
120called on this port and should return a character or {{#!eof}}. If it
121is not provided or {{#f}}, {{READ-CHAR}} will be used instead and the
122created port object handles peeking automatically (by calling {{READ}}
123and buffering the character).
124* {{read-bytevector}} is called when {{read-bytevector}} or {{read-string!}} is called (or the
125higher-level non-mutating {{read-string}} and {{read-bytevector}}). It will be invoked with 4
126arguments: the port created by {{make-input-port}}, the number of
127bytes to read, a bytevector to read into (which may be
128assumed to be big enough to hold the data) and the offset into the
129buffer at which to put the data to read. It should return the number
130of bytes that have successfully been read, which should always be
131equal to the requested bytes unless EOF was hit, in which case it can
132be less. If this procedure is not provided or {{#f}}, the buffer will
133be filled by repeated reads to {{READ-CHAR}}.
134* {{READ-LINE}} is called when {{read-line}} is called. It will be
135invoked with two arguments: the port created by {{make-input-port}}
136and the maximum number of characters to read (or {{#f}}). If this
137procedure is not provided or {{#f}}, the buffer will be filled by
138repeated reads to {{READ-CHAR}}.
139
140All the optional procedures except for {{PEEK-CHAR}} are responsible
141for updating the port's position, which currently can only be done via
142low-level slot accessors like {{##sys#setslot}}; slot 4 is the row
143number (ie, the line) and slot 5 is the column number (ie, the
144character on the line). If the port's positions are not updated,
145{{port-position}} won't work.
146
147Note that reading binary input from a custom input is only possible
148when the {{read-bytevector}} operation is given, as byte-input can
149currently not ben synthesized from character-input operations.
150
151
152==== make-output-port
153
154<procedure>(make-output-port WRITE CLOSE #!key force-output)</procedure>
155
156Returns a custom output port. Common operations on this port are handled
157by the given parameters, which should be procedures. {{WRITE}} is
158called when output is sent to the port and receives a single argument,
159a string. {{CLOSE}} is called when the port is closed and should
160be a procedure of no arguments. {{force-output}} (if provided) is called
161for flushing the output port.
162
163
164==== with-error-output-to-port
165
166<procedure>(with-error-output-to-port PORT THUNK)</procedure>
167
168Call procedure {{THUNK}} with the current error output-port
169temporarily bound to {{PORT}}.
170
171
172==== with-input-from-port
173
174<procedure>(with-input-from-port PORT THUNK)</procedure>
175
176Call procedure {{THUNK}} with the current input-port temporarily
177bound to {{PORT}}.
178
179
180=== String-port extensions
181
182==== call-with-input-string
183
184<procedure>(call-with-input-string STRING PROC)</procedure>
185
186Calls the procedure {{PROC}} with a single argument that is a
187string-input-port with the contents of {{STRING}}.
188
189
190==== call-with-output-string
191
192<procedure>(call-with-output-string PROC)</procedure>
193
194Calls the procedure {{PROC}} with a single argument that is a
195string-output-port. Returns the accumulated output-string.
196
197
198==== with-input-from-string
199
200<procedure>(with-input-from-string STRING THUNK)</procedure>
201
202Call procedure {{THUNK}} with the current input-port temporarily
203bound to an input-string-port with the contents of {{STRING}}.
204
205
206==== with-output-to-string
207
208<procedure>(with-output-to-string THUNK)</procedure>
209
210Call procedure {{THUNK}} with the current output-port temporarily
211bound to a string-output-port and return the accumulated output string.
212
213==== with-error-output-to-string
214
215<procedure>(with-error-output-to-string THUNK)</procedure>
216
217Call procedure {{THUNK}} with the current error output-port
218temporarily bound to a string-output-port and return the accumulated
219output string.
220
221
222=== Port iterators
223
224==== port-for-each
225
226<procedure>(port-for-each FN THUNK)</procedure>
227
228Apply {{FN}} to successive results of calling the zero argument procedure {{THUNK}} (typically {{read}}) until it returns {{#!eof}}, discarding the results.
229
230==== port-map
231
232<procedure>(port-map FN THUNK)</procedure>
233
234Apply {{FN}} to successive results of calling the zero argument procedure {{THUNK}} (typically {{read}}) until it returns {{#!eof}}, returning a list of the collected results.
235
236==== port-fold
237
238<procedure>(port-fold FN ACC THUNK)</procedure>
239
240Apply {{FN}} to successive results of calling the zero argument procedure {{THUNK}}, (typically {{read}}) passing the {{ACC}} value as the second argument. The {{FN}} result becomes the new {{ACC}} value. When {{THUNK}} returns {{#!eof}}, the last {{FN}} result is returned.
241
242==== copy-port
243
244<procedure>(copy-port FROM TO [READ [WRITE]])</procedure>
245
246Reads all remaining data from port {{FROM}} using the reader procedure
247{{READ}} and writes it to port {{TO}} using the writer procedure
248{{WRITE}}. {{READ}} defaults to {{read-char}} and {{WRITE}} to
249{{write-char}}. Note that this procedure does not check {{FROM}} and
250{{TO}} for being ports, so the reader and writer procedures may
251perform arbitrary operations as long as they can be invoked
252as {{(READ FROM)}} and {{(WRITE X TO)}}, respectively.
253{{copy-port}} returns an undefined value.
254
255{{copy-port}} was introduced in CHICKEN 4.6.0.
256
257=== Funky ports
258
259==== make-bidirectional-port
260
261<procedure>(make-bidirectional-port INPUT-PORT OUTPUT-PORT)</procedure>
262
263Returns a joint input/output port that proxies port operations to the
264given {{INPUT-PORT}} and {{OUTPUT-PORT}}, respectively. This port
265satisfies both {{input-port?}} and {{output-port?}}, and its two
266directions may be closed independently.
267
268==== make-broadcast-port
269
270<procedure>(make-broadcast-port PORT ...)</procedure>
271
272Returns a custom output port that emits everything written into it to
273the ports given as {{PORT ...}}. Closing the broadcast port does not close
274any of the argument ports.
275
276==== make-concatenated-port
277
278<procedure>(make-concatenated-port PORT1 PORT2 ...)</procedure>
279
280Returns a custom input port that reads its input from {{PORT1}}, until it
281is empty, then from {{PORT2}} and so on. Closing the concatenated port
282does not close any of the argument ports.
283
284
285---
286Previous: [[Module (chicken plist)]]
287
288Next: [[Module (chicken pretty-print)]]